package yanyan.com.tochar.funsactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.gs.buluo.common.widget.LoadingDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import yanyan.com.tochar.FunsPixelImgActivity;
import yanyan.com.tochar.R;
import yanyan.com.tochar.adpter.FullyGridLayoutManager;
import yanyan.com.tochar.adpter.GridImageAdapter;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.PictureSelectUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * 火车票生成
 */
public class FunsImageToUrlActivity extends Activity {
    private RecyclerView recyclerView;
    private GridImageAdapter adapter;
    private List imgList= new ArrayList<>();//图片集合

   private OkHttpClient okHttpClient=new OkHttpClient();

   private EditText resEd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_image_url);
        recyclerView=findViewById(R.id.image_url_recycler);
        resEd=findViewById(R.id.image_url_res);

        //初始化图片选择
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new GridImageAdapter(this, new GridImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                PictureSelectUtil.selectPohto(FunsImageToUrlActivity.this,3306);
            }
        });
        adapter.setList(imgList);
        adapter.setSelectMax(1);
        recyclerView.setAdapter(adapter);
        //统计各功能
        CoreUtil.statisticalFuns(this, "funs_image_url");
    }

    /**
     * 选择图片事件
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            imgList = PictureSelector.obtainMultipleResult(data);
            adapter.setList(imgList);
            adapter.notifyDataSetChanged();
        }
    }


    public void imageToUrl(View view){
        if(imgList!=null && imgList.size()>0){
            LocalMedia localMedia= (LocalMedia) imgList.get(0);
            String path="";
            if(CoreUtil.isAndroidQ()){
                path=localMedia.getAndroidQToPath();
            }else {
                path= localMedia.getPath();
            }

            //创建OkHttpClient对象(前提是导入了okhttp.jar和okio.jar)
            File imageFile=new File(path);
            String FileName=PictureSelectUtil.grentFileName()+".png";
            RequestBody fileBody=RequestBody.create(MediaType.parse("application/octet-stream"),imageFile);
            RequestBody requestBody=new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("filename",FileName,fileBody).
                            addFormDataPart("name","pic_path").build();
            Request request=new Request.Builder()
                    .url("http://pic.sogou.com/pic/upload_pic.jsp")
                    .post(requestBody)
                    .build();

            final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();

            OkHttpClient okHttpClient  = httpBuilder
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(15, TimeUnit.SECONDS).build();

            LoadingDialog.getInstance().show(FunsImageToUrlActivity.this, "正在上传", false);//加载
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    LoadingDialog.getInstance().dismissDialog();
                    ToastUtil.showLongToast(FunsImageToUrlActivity.this,"上传错误:"+e.getMessage());

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    LoadingDialog.getInstance().dismissDialog();
                    String resUrl=response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            resEd.setText(resUrl);
                            CoreUtil.copy(resUrl,FunsImageToUrlActivity.this);
                            ToastUtil.showLongToast(FunsImageToUrlActivity.this,"转换成功,且已复制到剪切板");
                        }
                    });

                }
            });


        }
    }

}
